//引入购物车纯函数
import { ADD_CART } from "../constants";
//初始化
const initCartlist = [];
//函数
const cartlist = (state=initCartlist,action) =>{
    const {type,data} = action;
    switch(type){
      case ADD_CART://添加购物车
      let {id} = data;
      let index = state.findIndex(value=> value.id===id)
      if(index!==-1){
        state[index].num++;
        return [...state]
      }
      else{
        return [...state,data]
      }
      default:
          return state;
    }
}

export default cartlist
